package com.bng8.superstar_cms.controller;

import com.bng8.superstar_cms.domain.common.PageQuery;
import com.bng8.superstar_cms.domain.common.PageResult;
import com.bng8.superstar_cms.domain.common.ResponseResult;
import com.bng8.superstar_cms.domain.dto.SupMissionDTO;
import com.bng8.superstar_cms.domain.dto.SupMissionQueryDTO;
import com.bng8.superstar_cms.domain.vo.SupMissionVO;
import com.bng8.superstar_cms.service.SupMissionService;
import com.bng8.superstar_cms.util.InsertValidationGroup;
import com.bng8.superstar_cms.util.UpdateValidationGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * ClassName: MissionController
 * Package: com.bng8.superstar_cms.controller
 * Date: 2023/9/4 9:00
 * Author: 安知（13921877634）
 */
@Slf4j
@Validated
@RestControllerAdvice
@RequestMapping("/api/mission")
public class MissionController {
    @Autowired
    private SupMissionService missionService;

    /**
     * 新增任务信息
     *
     * @param missionDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "mission-cache", allEntries = true)
    public ResponseResult addMissionInfo(@Validated(InsertValidationGroup.class) @RequestBody SupMissionDTO missionDTO) {
        missionService.save(missionDTO);
        return ResponseResult.success("新增成功");
    }

    /**
     * 更新任务信息
     *
     * @param missionDTO
     * @return
     */
    @PutMapping("/{id}")
    @CacheEvict(cacheNames = "mission-cache", allEntries = true)
    public ResponseResult updateMission(@NotNull @PathVariable("id") String id,
                                        @Validated(UpdateValidationGroup.class) @RequestBody SupMissionDTO missionDTO) {
        missionService.update(id, missionDTO);
        return ResponseResult.success("更新成功");
    }

    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "mission-cache", allEntries = true)
    public ResponseResult deleteMission(@NotNull @PathVariable("id") String id) {
        missionService.deleteById(id);
        return ResponseResult.success("删除成功");
    }

    /**
     * 根据用户名分页查询任务信息
     *
     * @param pageNo
     * @param pageSize
     * @param query
     * @return
     */
    @GetMapping
    @Cacheable(cacheNames = "mission-cache")
    public ResponseResult<PageResult> getMissionByChallenger(@NotNull Integer pageNo, @NotNull Integer pageSize,
                                                             @Validated SupMissionQueryDTO query) {

        // 构造查询条件
        PageQuery<SupMissionQueryDTO> pageQuery = new PageQuery<>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        pageQuery.setQuery(query);

        // 查询
        PageResult<List<SupMissionDTO>> pageResult =
                missionService.query(pageQuery);

        // 实体转换
        List<SupMissionVO> missionVOList = Optional
                .ofNullable(pageResult.getData())
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(supMissionDTO -> {
                    SupMissionVO supMissionVO = new SupMissionVO();
                    BeanUtils.copyProperties(supMissionDTO, supMissionVO);
                    return supMissionVO;
                })
                .collect(Collectors.toList());

        // 封装返回结果
        PageResult<List<SupMissionVO>> result = new PageResult<>();
        BeanUtils.copyProperties(pageResult, result);
        result.setData(missionVOList);

        return ResponseResult.success(result);
    }
}
